package com.gxgy.service.impl;

import java.util.List;

import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gxgy.base.BaseAllMapper;
import com.gxgy.entity.Student;
import com.gxgy.service.StudentService;
import com.gxgy.uilt.BaseUtil;
import com.gxgy.uilt.Id;
import com.gxgy.uilt.IfVersionException;
import com.gxgy.uilt.RandomId;


/**
 * @description: 学生 表
 * @author: Qin Fengang
 * @date: 2021-11-19
 */
@Service
public class StudentServiceImpl extends BaseAllMapper implements StudentService {

	
	/**
	 * @description: 学生 表  查询所有
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public List<Student> queryAllStudent() {
		return studentMapper.selectList(null);
	}


	/**
	 * @description: 学生 表  添加
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public Integer addStudent(String studentNumber, String studentName, String studentSex, Integer studentAge,
			String studentYear, String studentClass, String createTime,Model model) {
		if (studentNumber.isEmpty()) {
			model.addAttribute("msg","学号不能为空");
			throw new IfVersionException("学号不能为空");
		}
		if (studentName.isEmpty()) {
			model.addAttribute("msg","学生姓名不能为空");
			throw new IfVersionException("学生姓名不能为空");
		}
		if (studentSex.isEmpty()) {
			model.addAttribute("msg","学生性别不能为空");
			throw new IfVersionException("学生性别不能为空");
		}
		if (studentAge == null) {
			model.addAttribute("msg","学生年龄不能为空");
			throw new IfVersionException("学生年龄不能为空");
		}
		if (studentYear.isEmpty()) {
			model.addAttribute("msg","学生年级不能为空");
			throw new IfVersionException("学生年级不能为空");
		}
		if (studentClass.isEmpty()) {
			model.addAttribute("msg","学生班级不能为空");
			throw new IfVersionException("学生班级不能为空");
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			throw new IfVersionException("创建时间级不能为空");
		}
		QueryWrapper<Student> queryByNumber = new QueryWrapper<Student>()
			.eq(Student.STUDENT_NUMBER, studentNumber);
		Student returnByNumber = studentMapper.selectOne(queryByNumber);
		if (returnByNumber != null) {
			model.addAttribute("msg","该学号已存在");
			throw new IfVersionException("学号已存在");
		}
		String id = BaseUtil.getBaseId();
		Student student = new Student();
		student.setId(Id.student_+id);
		student.setStudentNumber(studentNumber);
		student.setStudentName(studentName);
		if (studentSex.equals("男")) {
			student.setStudentSex(0);
		}
		student.setStudentSex(1);
		student.setStudentAge(studentAge);
		student.setStudentYear(studentYear);
		student.setStudentClass(studentClass);
		student.setStudentClass(studentClass);
		student.setCreateTime(createTime);
		return studentMapper.insert(student);
	}


	/**
	 * @description: 学生 表 查询 通过学号
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public Student queryByNumber(String studentNumber) {
		QueryWrapper<Student> wrapper = new QueryWrapper<Student>()
				.eq(Student.STUDENT_NUMBER, studentNumber);
		return studentMapper.selectOne(wrapper);
	}


	/**
	 * @description: 学生 表 查询 通过id
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public Student queryById(String id) {
		QueryWrapper<Student> wrapper = new QueryWrapper<Student>()
				.eq(Student.ID, id);
		
		return studentMapper.selectOne(wrapper);
	}

	/**
	 * @description: 学生 表 修改  通过id
	 * @author: Qin Fengang
	 * @date: 2021-11-19
	 */
	@Override
	public Integer updateStudent(String id, String studentNumber, String studentName, String studentSex,
			Integer studentAge, String studentYear, String studentClass, String createTime, Model model) {
		if (studentNumber.isEmpty()) {
			model.addAttribute("msg","学号不能为空");
			throw new IfVersionException("学号不能为空");
		}
		if (studentName.isEmpty()) {
			model.addAttribute("msg","学生姓名不能为空");
			throw new IfVersionException("学生姓名不能为空");
		}
		if (studentSex.isEmpty()) {
			model.addAttribute("msg","学生性别不能为空");
			throw new IfVersionException("学生性别不能为空");
		}
		if (studentAge == null) {
			model.addAttribute("msg","学生年龄不能为空");
			throw new IfVersionException("学生年龄不能为空");
		}
		if (studentYear.isEmpty()) {
			model.addAttribute("msg","学生年级不能为空");
			throw new IfVersionException("学生年级不能为空");
		}
		if (studentClass.isEmpty()) {
			model.addAttribute("msg","学生班级不能为空");
			throw new IfVersionException("学生班级不能为空");
		}if (createTime.isEmpty()) {
			model.addAttribute("msg","创建时间不能为空");
			throw new IfVersionException("创建时间级不能为空");
		}
		QueryWrapper<Student> queryByNumber = new QueryWrapper<Student>()
			.eq(Student.STUDENT_NUMBER, studentNumber);
		Student returnByNumber = studentMapper.selectOne(queryByNumber);
		if (returnByNumber != null) {
			model.addAttribute("msg","该学号已存在");
			throw new IfVersionException("学号已存在");
		}
		Student student = new Student();
		student.setId(id);
		student.setStudentNumber(studentNumber);
		student.setStudentName(studentName);
		if (studentSex.equals("男")) {
			student.setStudentSex(0);
		}
		student.setStudentSex(1);
		student.setStudentAge(studentAge);
		student.setStudentYear(studentYear);
		student.setStudentClass(studentClass);
		student.setStudentClass(studentClass);
		student.setCreateTime(createTime);
		UpdateWrapper<Student> wrapper = new UpdateWrapper<Student>()
				.eq(Student.ID, id);
		return studentMapper.update(student, wrapper);
	}

}
